<?php

class TransferController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $session = new Zend_Session_Namespace('Zend_Auth');
        $this->sess = $session->data;

        if(!isset($this->sess)){
            $param = array(
            'action' => 'login',
            'controller' => 'index',
            'msg' => 'Session Timeout silahkan login kembali !'
            );
            $this->_redirect($this->view->url($param, 'default', true));
        }
        
        $this->_helper->layout->setLayout('transfer');

        $this->request = $this->getRequest();
        $this->param = $this->request->getParams();

        $this->member = new Application_Model_Member();
        $newMembers = $this->member->getNewMembers(20);
        $this->view->newMembers = $newMembers;

        $rewardMember = $this->member->getRewardMembers(20);
        $this->view->rewardMember = $rewardMember;
    }

    public function indexAction()
    {
        // action body
        //$this->render('point');
    }

    //Transfer Point
    public function transferpointAction(){

        $memberId = $this->sess->member_id;
        $accNum = $this->sess->member_code;

        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData  = $this->_request->getPost();

            
            $fromAcc = $formData['fromAcc'];
            $toRadio = $formData['toRadio'];
            $toAcc = (isset($formData['toAcc'])) ? $formData['toAcc'] : "";
            $toOtherAcc = (isset($formData['toOtherAcc'])) ? $formData['toOtherAcc'] : "";
            $cbSave = (isset($formData['cbSave'])) ? $formData['cbSave'] : "";
            $saveAs = (isset($formData['saveAs'])) ? $formData['saveAs'] : "";
            $amount = $formData['amount'];
            $remark = $formData['remark'];

            $toAcc = ($toRadio == 1) ? $toAcc : $toOtherAcc;
    


            $errorMsg = "";


            $accModel = new Application_Model_Rekening();
            //Pastikan Balance
            $getBalance = $accModel->getTotalBalance($fromAcc);
            $balance = $getBalance["balance"];

            if ($amount > $balance){
                $errorMsg .= "* Jumlah Transfer lebih besar daripada jumlah saldo.<br>";
            }


            //cek minimum saldo
	    /*if ($minSaldo > $balance || $minSaldo > ($balance - ($transfer+$biaya))){
                $this->view->errorMsg .= "* Minimum Saldo tidak mencukupi.<br>";
                //return;
                $err = 1;
            }
             * 
             */

            //
            if ($toRadio == 2){
                // Cek Account Tujuan
                $dataAcc = $accModel->isExistAccount($toOtherAcc);
                if ($dataAcc["cnt"] == 0){
                    $errorMsg .= "* Nomor Akun Tujuan tidak terdaftar.<br>";
                    
                }
            }


            if ($errorMsg == ""){

                $modelTransfer = new Application_Model_Transfer();
                $data = $modelTransfer->transferPoint($memberId, $fromAcc, $toAcc, $amount, $remark);

                $code = $data['code'];
                $msg = $data['msg'];

                if($code == 0){
                        //Jika disave
                        if ($cbSave != ""){
                            $dataTrans = array(
                                'account_num' => $toOtherAcc,
                                'member_id' => $memberId,
                                'trans_name' => $saveAs,
                                'trans_type_id' => 11
                                );

                            $save = $modelTransfer->saveToPersonalTrans($dataTrans);
                        }

                        $this->view->code = $code;
                        $this->view->msg = $msg;
                }else{
                    $this->view->code = '1';
                    $this->view->msg = "ERROR ".$msg;
                }
            }else{
                $this->view->code = '1';
                $this->view->msg = "ERROR ".$errorMsg;
            }

        }else{
            $modelAcc = new Application_Model_Rekening();
            $accountList = $modelAcc->getAccounts($accNum);
            $accPersonalList = $modelAcc->getPersonalAcc($memberId, "trans_type_id = 11");

            $modelFunc = new Application_Model_Functions();
            $this->view->today =  $modelFunc->gatDayIndoName(date('l')).', '.date('d F Y H:i:s');
            $this->view->accountList = $accountList;
            $this->view->accPersonalList = $accPersonalList;
        }
    }


    public function daftartransferAction(){

        $memberId = $this->sess->member_id;
        $accNum = $this->sess->member_code;

        if ($this->getRequest()->isPost()) {

            $this->_helper->layout->disableLayout();
            $formData  = $this->_request->getPost();


            $fromAcc = $formData['fromAcc'];
            $toRadio = $formData['toRadio'];
            $toAcc = (isset($formData['toAcc'])) ? $formData['toAcc'] : "";
            $toOtherAcc = (isset($formData['toOtherAcc'])) ? $formData['toOtherAcc'] : "";
            $cbSave = (isset($formData['cbSave'])) ? $formData['cbSave'] : "";
            $saveAs = (isset($formData['saveAs'])) ? $formData['saveAs'] : "";
            $amount = $formData['amount'];
            $remark = $formData['remark'];

            $toAcc = ($toRadio == 1) ? $toAcc : $toOtherAcc;



            $errorMsg = "";


            $accModel = new Application_Model_Rekening();
            //Pastikan Balance
            $getBalance = $accModel->getTotalBalance($fromAcc);
            $balance = $getBalance["balance"];

            if ($amount > $balance){
                $errorMsg .= "* Jumlah Transfer lebih besar daripada jumlah saldo.<br>";
            }


            //cek minimum saldo
	    /*if ($minSaldo > $balance || $minSaldo > ($balance - ($transfer+$biaya))){
                $this->view->errorMsg .= "* Minimum Saldo tidak mencukupi.<br>";
                //return;
                $err = 1;
            }
             *
             */

            //
            if ($toRadio == 2){
                // Cek Account Tujuan
                $dataAcc = $accModel->isExistAccount($toOtherAcc);
                if ($dataAcc["cnt"] == 0){
                    $errorMsg .= "* Nomor Akun Tujuan tidak terdaftar.<br>";

                }
            }


            if ($errorMsg == ""){

                $modelTransfer = new Application_Model_Transfer();
                $data = $modelTransfer->transferPoint($memberId, $fromAcc, $toAcc, $amount, $remark);

                $code = $data['code'];
                $msg = $data['msg'];

                if($code == 0){
                        //Jika disave
                        if ($cbSave != ""){
                            $dataTrans = array(
                                'account_num' => $toOtherAcc,
                                'member_id' => $memberId,
                                'trans_name' => $saveAs,
                                'trans_type_id' => 0
                                );

                            $save = $modelTransfer->saveToPersonalTrans($dataTrans);
                        }

                        $this->view->code = $code;
                        $this->view->msg = $msg;
                }else{
                    $this->view->code = '1';
                    $this->view->msg = "ERROR ".$msg;
                }
            }else{
                $this->view->code = '1';
                $this->view->msg = "ERROR ".$errorMsg;
            }

        }else{
            $modelAcc = new Application_Model_Rekening();
            $accountList = $modelAcc->getAccounts($accNum);
            $accPersonalList = $modelAcc->getPersonalAcc($memberId, "trans_type_id = 0");

            $modelFunc = new Application_Model_Functions();
            $this->view->today =  $modelFunc->gatDayIndoName(date('l')).', '.date('d F Y H:i:s');
            $this->view->accountList = $accountList;
            $this->view->accPersonalList = $accPersonalList;
        }
    }

    

}

